package jp.yyoo.signalfilter;


public class CircularBuffer 
{
	public static int BUFFER_SIZE = 1024;
	
	private double[] buf = new double[BUFFER_SIZE];
	private int length;
	private int top;
	private int mask;
	
	public CircularBuffer( int length ) 
	{
		top = 0;
		this.length = length;
		length = SignalFilterUtil.CeilPower2(length);
		mask = length - 1;
	}
	
	public void insert( double x )
	{
		--this.top;
		this.top &= this.mask;
		this.buf[this.top] = x;
	}
	
	public double getValueAt( int index )
	{
		return buf[(top + index) & mask];
	}
	public void setValueAt( int index, double value )
	{
		buf[ ( top + index ) & mask] = value;
	}
	
	public int getCount(){return length;}
}